package com.astrob.activitys;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.astrob.TileSource.ArcgisMapsTileSource;
import com.astrob.TileSource.BaiDRMapsTileSource;
import com.astrob.TileSource.GGSRMapsTileSource;
import com.astrob.TileSource.GTSRMapsTileSource;
import com.astrob.TileSource.GaoDRMapsTileSource;
import com.astrob.TileSource.GoogleMapsTileSource;
import com.astrob.TileSource.GoogleSRMapsTileSource;
import com.astrob.TileSource.MicrosoftMapsTileSource;
import com.astrob.TileSource.OCMDGXGaoDRMapsTileSource;
import com.astrob.TileSource.OCMDGXMapsTileSource;
import com.astrob.TileSource.TXRMapsTileSource;
import com.astrob.TileSource.TiandituMapsTileSource;
import com.astrob.model.Msg;
import com.astrob.nhcmap.R;
import com.astrob.util.SomeDataMapManger;
import com.astrob.view.ZoomView;

import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.Tag;
import org.mapsforge.core.util.Parameters;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.util.AndroidPreferences;
import org.mapsforge.map.android.util.AndroidUtil;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.datastore.PointOfInterest;
import org.mapsforge.map.layer.GroupLayer;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.hills.HillsRenderConfig;
import org.mapsforge.map.layer.hills.MemoryCachingHgtReaderTileSource;
import org.mapsforge.map.layer.hills.SimpleShadingAlgorithm;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.model.common.PreferencesFacade;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;
import org.mapsforge.map.rendertheme.XmlRenderTheme;
import org.mapsforge.map.rendertheme.XmlRenderThemeStyleMenu;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.api.IMapView;
import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.tileprovider.cachemanager.CacheManager;
import org.osmdroid.tileprovider.tilesource.ITileSource;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.FolderOverlay;
import org.osmdroid.views.overlay.Marker;
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import org.osmdroid.views.overlay.compass.CompassOverlay;
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider;
import org.osmdroid.views.overlay.gestures.RotationGestureOverlay;
import org.osmdroid.views.overlay.gridlines.LatLonGridlineOverlay;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
import org.osmdroid.views.overlay.simplefastpoint.LabelledGeoPoint;
import org.osmdroid.views.overlay.simplefastpoint.SimpleFastPointOverlay;
import org.osmdroid.views.overlay.simplefastpoint.SimpleFastPointOverlayOptions;
import org.osmdroid.views.overlay.simplefastpoint.SimplePointTheme;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


//http://www.tuicool.com/articles/Rr2qAbn

public class OSMActivity extends Activity implements View.OnClickListener {
//public class OSMActivity extends MapViewerTemplateRuntimePermissions implements View.OnClickListener {

    public static Intent createIntent(Context context) {
        return new Intent(context, OSMActivity.class);
    }

    private ZoomView mZoom;
    private TextView tv_tile;

    private MapView mMapView;
    private CompassOverlay mCompassOverlay;
    private ScaleBarOverlay mScaleBarOverlay;
    private MapController mMapController;
    private RotationGestureOverlay mRotationGestureOverlay;
    private CustomPointOverlay mGisOverlay = null;
    private MyLocationNewOverlay mLocationOverlay;

    private int _tileMode = 3;//0-OSM瓦片地图 1-GoogleMap 2-GoogleMapSat 3-离线矢量图
    private GeoPoint _geopoint = new GeoPoint(39.986250, 116.400025);//北京

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.osm_activity);
        mMapView = (MapView) findViewById(R.id.mapView);
        mZoom = (ZoomView) findViewById(R.id.id_zoomview);
        mZoom.setOnClickListener(this);
        findViewById(R.id.ibtn_local).setOnClickListener(this);
        findViewById(R.id.tv_tile).setOnClickListener(this);
        tv_tile = (TextView) findViewById(R.id.tv_tile);

        setTileMode1(_tileMode);

        //让瓦片适应不同像素密度
        mMapView.setTilesScaledToDpi(true);

        //添加指南针
        mCompassOverlay = new CompassOverlay(OSMActivity.this,
                new InternalCompassOrientationProvider(OSMActivity.this), mMapView);
        mMapView.getOverlays().add(mCompassOverlay);
        mCompassOverlay.enableCompass();

        //添加比例尺
        mScaleBarOverlay = new ScaleBarOverlay(mMapView);
        mMapView.getOverlays().add(mScaleBarOverlay);
        mScaleBarOverlay.setAlignBottom(true);//底部显示
        mScaleBarOverlay.setLineWidth(1 * (getResources().getDisplayMetrics()).density);
        mScaleBarOverlay.setMaxLength(0.85f);

        mMapController = (MapController) mMapView.getController();//获取MapView控制器
        mMapController.setCenter(_geopoint);//设置地图中心
        mMapView.setMaxZoomLevel(20);
        mMapView.setMinZoomLevel(6);
        mMapController.setZoom(15);//设置缩放级别
        mMapView.setBuiltInZoomControls(false);//设置缩放按钮可见
        mMapView.setMultiTouchControls(true);//设置多指触控可用
        mMapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速(绘制轨迹时需要)

        //设置导航图标
        mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this), mMapView);
        mMapView.getOverlays().add(this.mLocationOverlay);
        mLocationOverlay.enableMyLocation();  //设置可视

        //地图可旋转
        RotationGestureOverlay mRotationGestureOverlay = new RotationGestureOverlay(this, mMapView);
        mRotationGestureOverlay.setEnabled(true);
        mMapView.getOverlays().add(mRotationGestureOverlay);

        mMapView.setMapListener(mapListener);
        LatLonGridlineOverlay.fontSizeDp = 16;
        LatLonGridlineOverlay.fontColor = Color.argb(255, 0, 255, 0);
        LatLonGridlineOverlay.backgroundColor = Color.BLACK;
        LatLonGridlineOverlay.lineColor = LatLonGridlineOverlay.fontColor;
        updateGridlines();

        addMarker();
        addLine();
        addArea();

//        addOverlays();
        mGisOverlay = new CustomPointOverlay();
        mMapView.getOverlays().add(mGisOverlay);

        mMapView.setVisibility(View.GONE);
        findViewById(R.id.ibtn_local).setVisibility(View.GONE);
        findViewById(R.id.tv_tile).setVisibility(View.GONE);
        intMapsforge();
    }

    private void setTileMode2(int tileMode) {
        int size = TileSourceFactory.getTileSources().size();
        if (tileMode >= size) {
            tileMode = 0;
        }
        _tileMode = tileMode;
        ITileSource aTileSource = TileSourceFactory.getTileSources().get(tileMode);
        mMapView.setTileSource(aTileSource);
        Toast.makeText(this, aTileSource.name(), Toast.LENGTH_SHORT).show();

        tv_tile.setText("T" + tileMode);
        _geopoint = new GeoPoint(39.986250, 116.400025);//北京
        mMapView.getController().setZoom(18);
        mMapView.getController().setCenter(_geopoint);
    }

    private void setTileMode1(int tileMode) {
        if (tileMode > 3) {
            tileMode = 0;
        }
        _tileMode = tileMode;
        SomeDataMapManger.detach();
        switch (_tileMode) {
            default:
            case 0:
                mMapView.setTileSource(TileSourceFactory.MAPNIK);//（OCM等高）若不设置，则默认使用的是MAPNIK（OSM街道）
                tv_tile.setText("T0");
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                Toast.makeText(this, "OpenStreetMap-MAPNIK", Toast.LENGTH_SHORT).show();
                break;
            case 1: {
                String str1 = "http://mt0.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                String str2 = "http://mt1.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                String str3 = "http://mt2.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                String str4 = "http://mt3.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                GoogleMapsTileSource googleMapsTileSource = new GoogleMapsTileSource("GoogleNormal", 2, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});
                mMapView.setTileSource(googleMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T1");
                Toast.makeText(this, "GoogleMaps", Toast.LENGTH_SHORT).show();
            }
            break;
            case 2: {
                String str1 = "http://mt0.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                String str2 = "http://mt1.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                String str3 = "http://mt2.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                String str4 = "http://mt3.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-Sat", 2, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});
                mMapView.setTileSource(googleMapsSatTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T1");
                Toast.makeText(this, "Google-Sat", Toast.LENGTH_SHORT).show();
            }
            break;
            case 3: {
                SomeDataMapManger.initmapViewData(mMapView, OSMActivity.this);
                mMapView.setUseDataConnection(true);
                mMapView.getOverlayManager().getTilesOverlay().setEnabled(true);////是否显示地图数据源
//                _geopoint = new GeoPoint(25.05, 121.50);//台北
                _geopoint = new GeoPoint(32.0607297590, 118.7629586387);//江苏
                tv_tile.setText("T2");
                Toast.makeText(this, "OSM·离线地图", Toast.LENGTH_SHORT).show();
            }
            break;
        }
        mMapView.getController().setZoom(18);
        mMapView.getController().setCenter(_geopoint);
    }

//    01 卫星图：谷歌卫星 arcgis卫星 天地图卫星 微软卫星
//    10 混合图：谷歌卫星+谷歌道路；谷歌卫星+高德道路、 谷歌卫星+天地图道路
//    20 道路图：谷歌 高德 腾讯 百度
//    30 等高线图：

    private void setTileMode(int tileMode) {
        if (tileMode > 17) {
            tileMode = 0;
        }
        _tileMode = tileMode;
        SomeDataMapManger.detach();
        switch (_tileMode) {
            default:
            case 0: {
                //谷歌卫星
                String str1 = "http://mt0.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                String str2 = "http://mt1.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                String str3 = "http://mt2.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                String str4 = "http://mt3.google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&scale=2";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-Sat", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});
                mMapView.setTileSource(googleMapsSatTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T0");
                Toast.makeText(this, "谷歌卫星", Toast.LENGTH_SHORT).show();
            }
            break;
            case 1: {
                //arcgis卫星
                String str1 = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/";
                ArcgisMapsTileSource arcgisMapsTileSource = new ArcgisMapsTileSource("Arcgis-Sat", 3, 19, 256,
                        ".jpg", new String[]{str1});
                mMapView.setTileSource(arcgisMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T1");
                Toast.makeText(this, "arcgis卫星", Toast.LENGTH_SHORT).show();
            }
            break;
            case 2: {
                //天地图卫星
                String str1 = "http://t0.tianditu.cn/DataServer?T=img_w";
                String str2 = "http://t1.tianditu.cn/DataServer?T=img_w";
                String str3 = "http://t2.tianditu.cn/DataServer?T=img_w";
                String str4 = "http://t3.tianditu.cn/DataServer?T=img_w";
                String str5 = "http://t4.tianditu.cn/DataServer?T=img_w";
                TiandituMapsTileSource tiandituMapsTileSource = new TiandituMapsTileSource("Tianditu-Sat", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4, str5});
                mMapView.setTileSource(tiandituMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T2");
                Toast.makeText(this, "天地图卫星", Toast.LENGTH_SHORT).show();
            }
            break;
            case 3: {
                //微软卫星
                String str1 = "http://t1.tiles.ditu.live.com/tiles/";
                MicrosoftMapsTileSource microsoftMapsTileSource = new MicrosoftMapsTileSource("Microsoft-Sat", 3, 19, 256,
                        ".png", new String[]{str1});
                mMapView.setTileSource(microsoftMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T3");
                Toast.makeText(this, "微软卫星", Toast.LENGTH_SHORT).show();
            }
            break;
            case 4: {
                //谷歌卫星+谷歌道路
                String str1 = "http://mt0.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                String str2 = "http://mt1.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                String str3 = "http://mt2.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                String str4 = "http://mt3.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-DGX", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});

                str1 = "127.0.0.1:25412/getnotfake.act?";
                GoogleSRMapsTileSource googleSRMapsTileSource = new GoogleSRMapsTileSource("Google-SR", 3, 19, 256,
                        ".png", new String[]{str1});
                googleSRMapsTileSource.setMapsTileSource2(googleMapsSatTileSource);
                mMapView.setTileSource(googleSRMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T4");
                Toast.makeText(this, "谷歌卫星+谷歌道路", Toast.LENGTH_SHORT).show();
            }
            break;
            case 5: {
                //谷歌卫星+高德道路
                String str1 = "http://webst01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8";
                String str2 = "http://webst02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8";
                String str3 = "http://webst03.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8";
                String str4 = "http://webst04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-DGX", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});

                str1 = "127.0.0.1:25412/getnotfake.act?";
                GGSRMapsTileSource gGSRMapsTileSource = new GGSRMapsTileSource("GG-SR", 3, 19, 256,
                        ".png", new String[]{str1});
                gGSRMapsTileSource.setMapsTileSource2(googleMapsSatTileSource);
                mMapView.setTileSource(gGSRMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T5");
                Toast.makeText(this, "谷歌卫星+高德道路", Toast.LENGTH_SHORT).show();
            }
            break;
            case 6: {
                //谷歌卫星+天地图道路
                String str1 = "http://t0.tianditu.cn/DataServer?T=cia_w";
                String str2 = "http://t1.tianditu.cn/DataServer?T=cia_w";
                String str3 = "http://t2.tianditu.cn/DataServer?T=cia_w";
                String str4 = "http://t3.tianditu.cn/DataServer?T=cia_w";
                String str5 = "http://t4.tianditu.cn/DataServer?T=cia_w";
                GTSRMapsTileSource gTSRMapsTileSource = new GTSRMapsTileSource("GT-SR", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4, str5});
                mMapView.setTileSource(gTSRMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T6");
                Toast.makeText(this, "谷歌卫星+天地图道路", Toast.LENGTH_SHORT).show();
            }
            break;
            case 7: {
                //谷歌
                String str1 = "http://mt0.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                String str2 = "http://mt1.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                String str3 = "http://mt2.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                String str4 = "http://mt3.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&scale=2";
                GoogleMapsTileSource googleMapsTileSource = new GoogleMapsTileSource("GoogleNormal", 2, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});
                mMapView.setTileSource(googleMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T7");
                Toast.makeText(this, "GoogleMaps", Toast.LENGTH_SHORT).show();
            }
            break;
            case 8: {
                //高德道路
                String str1 = "http://wprd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str2 = "http://wprd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str3 = "http://wprd03.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str4 = "http://wprd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-DGX", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});

                str1 = "http://127.0.0.1:25412/getnotfake.act?";
                GaoDRMapsTileSource gaoDRMapsTileSource = new GaoDRMapsTileSource("GaoDR", 2, 19, 256,
                        ".png", new String[]{str1});
                mMapView.setTileSource(gaoDRMapsTileSource);
                gaoDRMapsTileSource.setMapsTileSource2(googleMapsSatTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T8");
                Toast.makeText(this, "高德道路", Toast.LENGTH_SHORT).show();
            }
            break;
            case 9: {
                //腾讯道路
                String str1 = "http://wprd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str2 = "http://wprd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str3 = "http://wprd03.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str4 = "http://wprd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-DGX", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});

                str1 = "http://127.0.0.1:25412/getnotfake.act?";
                TXRMapsTileSource tXRMapsTileSource = new TXRMapsTileSource("TXR", 2, 19, 256,
                        ".png", new String[]{str1});
                tXRMapsTileSource.setMapsTileSource2(googleMapsSatTileSource);
                mMapView.setTileSource(tXRMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T9");
                Toast.makeText(this, "腾讯道路", Toast.LENGTH_SHORT).show();
            }
            break;
            case 10: {
                //百度道路
                String str1 = "http://wprd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str2 = "http://wprd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str3 = "http://wprd03.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                String str4 = "http://wprd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-DGX", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});

//                str1 = "http://127.0.0.1:25412/getnotfake.act?";
//                BaiDRMapsTileSource baiDRMapsTileSource = new BaiDRMapsTileSource("TXR", 2, 19, 256,
//                        ".png", new String[]{str1});
//                baiDRMapsTileSource.setMapsTileSource2(googleMapsSatTileSource);
//                mMapView.setTileSource(baiDRMapsTileSource);
//                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
//                tv_tile.setText("T10");
//                Toast.makeText(this, "百度道路", Toast.LENGTH_SHORT).show();
            }
            break;
            case 11: {
                //OCM等高线
                String str1 = "http://a.tile.opencyclemap.org/cycle/";
                String str2 = "http://b.tile.opencyclemap.org/cycle/";
                String str3 = "http://c.tile.opencyclemap.org/cycle/";
                OCMDGXMapsTileSource oCMDGXMapsTileSource = new OCMDGXMapsTileSource("OCM-DGX", 8, 25, 256,
                        ".png", new String[]{str1, str2, str3});
                mMapView.setTileSource(oCMDGXMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T11");
                Toast.makeText(this, "OCM等高线", Toast.LENGTH_SHORT).show();
            }
            break;
            case 12: {
                //OSMSummer等高线
                String str1 = "http://s0.outdooractive.com/osm/OSMSummer/";
                String str2 = "http://s1.outdooractive.com/osm/OSMSummer/";
                String str3 = "http://s2.outdooractive.com/osm/OSMSummer/";
                String str4 = "http://s3.outdooractive.com/osm/OSMSummer/";
                OCMDGXMapsTileSource oCMDGXMapsTileSource = new OCMDGXMapsTileSource("OSMSummer-DGX", 3, 18, 256,
                        ".png", new String[]{str1, str2, str3, str4});
                mMapView.setTileSource(oCMDGXMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T12");
                Toast.makeText(this, "OSMSummer等高线", Toast.LENGTH_SHORT).show();
            }
            break;
            case 13: {
                //OSMWinter等高线
                String str1 = "http://s0.outdooractive.com/osm/OSMWinter/";
                String str2 = "http://s1.outdooractive.com/osm/OSMWinter/";
                String str3 = "http://s2.outdooractive.com/osm/OSMWinter/";
                String str4 = "http://s3.outdooractive.com/osm/OSMWinter/";
                OCMDGXMapsTileSource oCMDGXMapsTileSource = new OCMDGXMapsTileSource("OSMWinter-DGX", 3, 18, 256,
                        ".png", new String[]{str1, str2, str3, str4});
                mMapView.setTileSource(oCMDGXMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T13");
                Toast.makeText(this, "OSMWinter等高线", Toast.LENGTH_SHORT).show();
            }
            break;
            case 14: {
                //4U等高线
                String str1 = "http://4umaps.eu/";
                OCMDGXMapsTileSource oCMDGXMapsTileSource = new OCMDGXMapsTileSource("4U-DGX", 3, 18, 256,
                        ".png", new String[]{str1});
                mMapView.setTileSource(oCMDGXMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T14");
                Toast.makeText(this, "4U等高线", Toast.LENGTH_SHORT).show();
            }
            break;
            case 15: {
                //sigma等高线
                String str1 = "http://tiles1.sigma-dc-control.com/layer3/";
                String str2 = "http://tiles1.sigma-dc-control.com/layer7/";
                OCMDGXMapsTileSource oCMDGXMapsTileSource = new OCMDGXMapsTileSource("sigma-DGX", 3, 18, 256,
                        ".png", new String[]{str1, str2});
                mMapView.setTileSource(oCMDGXMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T15");
                Toast.makeText(this, "sigma等高线", Toast.LENGTH_SHORT).show();
            }
            break;
            case 16: {
                //谷歌地形
                String str1 = "http://mt0.google.cn/vt/lyrs=t@128&hl=zh-CN&gl=cn&src=app&s=Galileo";
                String str2 = "http://mt1.google.cn/vt/lyrs=t@128&hl=zh-CN&gl=cn&src=app&s=Galileo";
                String str3 = "http://mt2.google.cn/vt/lyrs=t@128&hl=zh-CN&gl=cn&src=app&s=Galileo";
                String str4 = "http://mt3.google.cn/vt/lyrs=t@128&hl=zh-CN&gl=cn&src=app&s=Galileo";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-DGX", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});
                mMapView.setTileSource(googleMapsSatTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T16");
                Toast.makeText(this, "谷歌地形", Toast.LENGTH_SHORT).show();
            }
            break;
            case 17: {
                //OCM等高线+高德道路
                String str1 = "http://mt0.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                String str2 = "http://mt1.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                String str3 = "http://mt2.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                String str4 = "http://mt3.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app";
                GoogleMapsTileSource googleMapsSatTileSource = new GoogleMapsTileSource("Google-DGX", 3, 19, 256,
                        ".png", new String[]{str1, str2, str3, str4});


                str1 = "http://127.0.0.1:25412/getnotfake.act?";
                OCMDGXGaoDRMapsTileSource oCMDGXGaoDRMapsTileSource = new OCMDGXGaoDRMapsTileSource("OCM-DGX-GaoDR", 3, 19, 256,
                        ".png", new String[]{str1});
                oCMDGXGaoDRMapsTileSource.setMapsTileSource2(googleMapsSatTileSource);
                mMapView.setTileSource(oCMDGXGaoDRMapsTileSource);
                _geopoint = new GeoPoint(39.986250, 116.400025);//北京
                tv_tile.setText("T17");
                Toast.makeText(this, "OCM等高线+高德道路", Toast.LENGTH_SHORT).show();
            }
            break;
        }
        mMapView.getController().setZoom(18);
        mMapView.getController().setCenter(_geopoint);
    }

    MapListener mapListener = new MapListener() {
        @Override
        public boolean onScroll(ScrollEvent scrollEvent) {
            updateGridlines();
            return false;
        }

        @Override
        public boolean onZoom(ZoomEvent zoomEvent) {
            updateGridlines();
            return false;
        }

        @Override
        public void longPress(Point pt) {
            if (pt == null) {
                return;
            }
        }

        //双击
        @Override
        public void doubleClick(Point pt) {
            if (pt == null) {
                return;
            }
        }

        //单击 单击后
        @Override
        public void singleClick(Point pt) {
            if (pt == null) {
                return;
            }

        }

        //移图后抬起
        @Override
        public void touchUp() {

        }

        //手指移动-切换到移图模式
        @Override
        public void moveMap() {

        }
    };

    private FolderOverlay activeLatLonGrid;
    private boolean _isShowGrid = false;

    //地图网格
    protected void updateGridlines() {
        if (!_isShowGrid)
            return;

        if (activeLatLonGrid != null) {
            mMapView.getOverlayManager().remove(activeLatLonGrid);
            activeLatLonGrid.onDetach(mMapView);
        }
        LatLonGridlineOverlay.backgroundColor = Color.BLACK;
        LatLonGridlineOverlay.fontColor = Color.WHITE;
        LatLonGridlineOverlay.lineColor = Color.GRAY;
        activeLatLonGrid = LatLonGridlineOverlay.getLatLonGrid(this, mMapView);
        mMapView.getOverlays().add(activeLatLonGrid);

    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case Msg.SHOWTIP:
                    break;
                case Msg.UPDATEMAP:
                    updateMapView();
                    break;
                case Msg.FINISHUPDATEMAP:
                    finishUpdatedMapView();
                    break;
            }
        }
    };

    private void updateMapView() {

    }

    private void finishUpdatedMapView() {
        mZoom.updateState();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_local:
                //定位当前的位置，并设置缩放级别
                mMapView.getController().setZoom(18);
                mMapView.getController().setCenter(_geopoint);
                break;
            case R.id.btnzoomin:
                mMapView.getController().zoomIn();
                mapView.getModel().mapViewPosition.zoomIn();
                break;
            case R.id.btnzoomout:
                mMapView.getController().zoomOut();
                mapView.getModel().mapViewPosition.zoomOut();
                break;
            case R.id.tv_tile:
                _tileMode++;
                setTileMode1(_tileMode);
                break;
        }
    }

    // MapMode -- 移图模式/导航模式
    public final int MOVING_MODE = 0;
    public final int NAVI_MODE = 1;
    private int mMapMode = -1;

    private void initWnd(int mapMode) {
        mMapMode = mapMode;

//        mLocationOverlay.isFollowLocationEnabled()
        if (MOVING_MODE == mapMode) {
            mLocationOverlay.disableFollowLocation();
        } else if (NAVI_MODE == mapMode) {
            mLocationOverlay.enableFollowLocation();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mLocationOverlay != null) {
            mLocationOverlay.disableMyLocation();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (activeLatLonGrid != null && mMapView != null)
            activeLatLonGrid.onDetach(mMapView);
        activeLatLonGrid = null;

        super.onDestroy();

    }

    //添加Marker
    private void addMarker() {
//        GeoPoint geopoint = new GeoPoint(39.986250, 116.400025);
        Marker marker = new Marker(mMapView);
        marker.setIcon(getResources().getDrawable(R.drawable.point01));//设置图标
        marker.setPosition(_geopoint);//设置位置
        marker.setAnchor(0.5f, 0.5f);//设置偏移量
        marker.setTitle("Titile");//设置标题
        marker.setSubDescription("SubDescription");//设置说明
        mMapView.getOverlays().add(marker);//添加marker到MapView
    }

    //连线(连线时务必关闭硬件加速，否则可能显示不出来连的线)
    private void addLine() {
        List<org.osmdroid.util.GeoPoint> points = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            points.add(new LabelledGeoPoint(_geopoint.getLatitude() + Math.random() * 0.5, _geopoint.getLongitude() + Math.random() * 0.5
                    , "Point #" + i));
        }

        //随机产生的20个点并连成线
        if (points.size() > 2) {
            org.osmdroid.views.overlay.Polyline Polyline = new org.osmdroid.views.overlay.Polyline();
            Polyline.setWidth(2);
            Polyline.setColor(0xFF1B7BCD);
            Polyline.setPoints(points);
            mMapView.getOverlays().add(Polyline);
        }

//计算边界值，定位边界
        final BoundingBox box = new BoundingBox(39, 116, 40, 117);
        // zoom to its bounding box
        mMapView.addOnFirstLayoutListener(new MapView.OnFirstLayoutListener() {

            @Override
            public void onFirstLayout(View v, int left, int top, int right, int bottom) {
                if (mMapView != null && mMapView.getController() != null) {
                    mMapView.getController().zoomTo(15);
//                    mMapView.zoomToBoundingBox(box, true);
                }

            }
        });
    }

    //画面
    private void addArea() {
        List<GeoPoint> points = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            points.add(new LabelledGeoPoint(_geopoint.getLatitude() + Math.random() * 0.5, _geopoint.getLongitude() + Math.random() * 0.5
                    , "Point #" + i));
        }

        //随机产生的20个点并连成面
        if (points.size() > 2) {
            org.osmdroid.views.overlay.Polygon polygon = new org.osmdroid.views.overlay.Polygon();
            polygon.setStrokeWidth(1);
            polygon.setFillColor(0x8032B5EB);
            polygon.setStrokeColor(Color.BLUE);
            polygon.setPoints(points);
            mMapView.getOverlays().add(polygon);
        }

//计算边界值，定位边界
        final BoundingBox box = new BoundingBox(37, -8, 42, -3);
        // zoom to its bounding box
        mMapView.addOnFirstLayoutListener(new MapView.OnFirstLayoutListener() {

            @Override
            public void onFirstLayout(View v, int left, int top, int right, int bottom) {
                if (mMapView != null && mMapView.getController() != null) {
                    mMapView.getController().zoomTo(13);
//                    mMapView.zoomToBoundingBox(box, true);
                }

            }
        });
    }

    //自定义瓦片缓存位置
    private void setSelfSaveDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "AAA");//新建文件夹
        if (dir.exists()) {
            File nomedia = new File(dir.getAbsoluteFile() + "/.nomedia");
            if (!nomedia.exists()) {
                try {
                    nomedia.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            dir.mkdirs();
            File file = dir.getAbsoluteFile();
            try {
                new File(file + "/.nomedia").createNewFile();
            } catch (Exception ex) {
                android.util.Log.e(IMapView.LOGTAG, "unable to create a nomedia file. downloaded tiles may be visible to the gallery.", ex);
            }
        }
//        OpenStreetMapTileProviderConstants.setCachePath(dir + "");//设置MapView的缓存路径
    }

    //离线地图下载
    private void downLoad() {
        CacheManager cacheManager = new CacheManager(mMapView);//获取下载器
        BoundingBox boundingBoxE6 = mMapView.getBoundingBox();//获取当前区域
        int tileNum = cacheManager.possibleTilesInArea(boundingBoxE6, 8, 16);//计算当前区域8-16级的瓦片数量
        final CacheManager.CacheManagerTask cacheManagerTask = cacheManager.downloadAreaAsync(this, boundingBoxE6, 8, 16,
                new CacheManager.CacheManagerCallback() {
                    @Override
                    public void updateProgress(int progress, int currentZoomLevel, int zoomMin, int zoomMax) {

                    }

                    @Override
                    public void downloadStarted() {

                    }

                    @Override
                    public void setPossibleTilesInArea(int total) {

                    }

                    @Override
                    public void onTaskFailed(int errors) {

                    }//下载

                    @Override
                    public void onTaskComplete() {
                        //下载完成后的回调
                    }
                });
    }

    //绘制5w个点的图层(太慢了)，方法1
    private void addOverlays() {
        // create 10k labelled points
        // in most cases, there will be no problems of displaying >100k points, feel free to try
        List<IGeoPoint> points = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            points.add(new LabelledGeoPoint(39.986 + Math.random() * 0.05, 116.40 + Math.random() * 0.05
                    , "Point #" + i));
        }

        // wrap them in a theme
        SimplePointTheme pt = new SimplePointTheme(points, true);

        // create label style
        Paint textStyle = new Paint();
        textStyle.setStyle(Paint.Style.FILL);
        textStyle.setColor(Color.parseColor("#0000ff"));
        textStyle.setTextAlign(Paint.Align.CENTER);
        textStyle.setTextSize(24);

        // set some visual options for the overlay
        // we use here MAXIMUM_OPTIMIZATION algorithm, which works well with >100k points
        SimpleFastPointOverlayOptions opt = SimpleFastPointOverlayOptions.getDefaultStyle()
                .setAlgorithm(SimpleFastPointOverlayOptions.RenderingAlgorithm.MAXIMUM_OPTIMIZATION)
                .setRadius(7).setIsClickable(true).setCellSize(15).setTextStyle(textStyle);

// create the overlay with the theme
        final SimpleFastPointOverlay sfpo = new SimpleFastPointOverlay(pt, opt);

        // onClick callback
        sfpo.setOnClickListener(new SimpleFastPointOverlay.OnClickListener() {
            @Override
            public void onClick(SimpleFastPointOverlay.PointAdapter points, Integer point) {
                Toast.makeText(mMapView.getContext()
                        , "You clicked " + ((LabelledGeoPoint) points.get(point)).getLabel()
                        , Toast.LENGTH_SHORT).show();
            }
        });

        // add overlay
        mMapView.getOverlays().add(sfpo);

        // zoom to its bounding box
        mMapView.addOnFirstLayoutListener(new MapView.OnFirstLayoutListener() {

            @Override
            public void onFirstLayout(View v, int left, int top, int right, int bottom) {
                if (mMapView != null && mMapView.getController() != null) {
                    mMapView.getController().zoomTo(15);
                    mMapView.zoomToBoundingBox(sfpo.getBoundingBox(), true);
                }

            }
        });

    }

    //自定义图层-方法2，可以画点线面
    public class CustomPointOverlay extends Overlay {

        private final Point mMapCoordsProjected = new Point();
        private final Point mMapCoordsTranslated = new Point();
        protected Paint mCirclePaint = new Paint();

        public CustomPointOverlay() {
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            //经纬度坐标到屏幕坐标的转换
            mapView.getProjection().toProjectedPixels(39.986250, 116.400025, mMapCoordsProjected);
            Projection pj = mapView.getProjection();
            pj.toPixelsFromProjected(mMapCoordsProjected, mMapCoordsTranslated);
            final float radius = 10L;
            mCirclePaint.setColor(Color.BLUE);
            mCirclePaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(mMapCoordsTranslated.x, mMapCoordsTranslated.y, radius, mCirclePaint);
        }

    }
/*
    //Mapsforge
    public static final LatLong LATLONG_JS = new LatLong(32.0607297590, 118.7629586387);

    @Override
    protected int getLayoutId() {
        return R.layout.osm_activity;
    }

    @Override
    protected int getMapViewId() {
        return R.id.id_mapViewM;
    }

    @Override
    protected MapPosition getDefaultInitialPosition() {
        return new MapPosition(LATLONG_JS, (byte) 12);
    }

    @Override
    protected void createLayers() {
        File demFolder = new File(Start.RUNDIR, "dem");
        HillsRenderConfig hillsConfig = null;
        if (!(demFolder.exists() && demFolder.isDirectory() && demFolder.canRead() && demFolder.listFiles().length > 0)) {
            hillsConfig = null;
        } else {
            // minimum setup for hillshading
            MemoryCachingHgtReaderTileSource hillTileSource = new MemoryCachingHgtReaderTileSource(demFolder, new SimpleShadingAlgorithm(), AndroidGraphicFactory.INSTANCE);
            hillTileSource.setEnableInterpolationOverlap(true);
            hillsConfig = new HillsRenderConfig(hillTileSource);
            // call after setting/changing parameters, walks filesystem for DEM metadata
            hillsConfig.indexOnThread();
        }

        TileRendererLayer tileRendererLayer = AndroidUtil.createTileRendererLayer(this.tileCaches.get(0),
                mapView.getModel().mapViewPosition, getMapFile(), getRenderTheme(), false, true, false,
                hillsConfig);
        mapView.getLayerManager().getLayers().add(tileRendererLayer);

        // needed only for samples to hook into Settings.
        setMaxTextWidthFactor();
    }

    @Override
    protected void createControls() {
        super.createControls();
    }

    @Override
    protected void createMapViews() {
        // Frame buffer HA2
        Parameters.FRAME_BUFFER_HA2 = true;
        super.createMapViews();

        mapView.getModel().mapViewPosition.setMapPosition(new MapPosition(LATLONG_JS, (byte) 12));
    }

    @Override
    protected void createTileCaches() {
        boolean persistent = true;
        this.tileCaches.add(AndroidUtil.createTileCache(this, getPersistableId(),
                mapView.getModel().displayModel.getTileSize(), this.getScreenRatio(),
                mapView.getModel().frameBufferModel.getOverdrawFactor(), persistent));
    }

    @Override
    protected String getMapFileName() {
        return "china.map";
    }

    @Override
    protected XmlRenderTheme getRenderTheme() {
        return InternalRenderTheme.OSMARENDER;
    }

    @Override
    protected File getMapFileDirectory() {
        String mapdir = Start.RUNDIR + "/mapsforge/";
        if (mapdir != null) {
            File file = new File(mapdir);
            if (file.exists() && file.isDirectory()) {
                return file;
            }
            throw new RuntimeException(file + " does not exist or is not a directory (configured in launch URI)");
        }
        return super.getMapFileDirectory();
    }

    protected void setMaxTextWidthFactor() {
        mapView.getModel().displayModel.setMaxTextWidthFactor(0.7f);
    }
 */

    private org.mapsforge.map.android.view.MapView mapView = null;
    private PreferencesFacade preferencesFacade;
    private XmlRenderThemeStyleMenu renderThemeStyleMenu;
    private List<TileCache> tileCaches = new ArrayList<TileCache>();

    private String getPersistableId() {
        return "OSMActivity2";
//        return this.getClass().getSimpleName();
    }

    private float getScreenRatio() {
        return 1.0f;//dp:px
    }

    private MapDataStore getMapFile() {
        String mapdir = Environment.getExternalStorageDirectory() + "/BHCMapper/mapsforge";
        return new MapFile(new File(mapdir, "china.map"));
    }

    private XmlRenderTheme getRenderTheme() {
        return InternalRenderTheme.DEFAULT;
    }

    private HillsRenderConfig hillsConfig = null;

    private HillsRenderConfig getHillsRenderConfig() {
        File demFolder = new File(Environment.getExternalStorageDirectory() + "/BHCMapper/", "dem");
        if (!(demFolder.exists() && demFolder.isDirectory() && demFolder.canRead() && demFolder.listFiles().length > 0)) {
            hillsConfig = null;
        } else {
            // minimum setup for hillshading
            MemoryCachingHgtReaderTileSource hillTileSource = new MemoryCachingHgtReaderTileSource(demFolder, new SimpleShadingAlgorithm(), AndroidGraphicFactory.INSTANCE);
            hillTileSource.setEnableInterpolationOverlap(false);
            hillsConfig = new HillsRenderConfig(hillTileSource);

            // call after setting/changing parameters, walks filesystem for DEM metadata
            hillsConfig.indexOnThread();
        }
        return hillsConfig;
    }

    private byte getZoomLevelMin() {
        return (byte) 0;
    }

    /**
     * @return the maximum zoom level of the map view.
     */
    private byte getZoomLevelMax() {
        return (byte) 24;
    }

    private void intMapsforge() {
        mapView = (org.mapsforge.map.android.view.MapView) findViewById(R.id.id_mapViewM);
        mapView.setVisibility(View.VISIBLE);
        //createSharedPreferences
        preferencesFacade = new AndroidPreferences(this.getSharedPreferences(getPersistableId(), MODE_PRIVATE));

        //createMapViews
        Parameters.FRAME_BUFFER_HA2 = true;

        mapView.getModel().init(preferencesFacade);//设置MapPosition
        mapView.setClickable(true);
        mapView.getMapScaleBar().setVisible(true);
        mapView.setBuiltInZoomControls(true);
        mapView.getMapZoomControls().setAutoHide(true);
        mapView.getMapZoomControls().setZoomLevelMin(getZoomLevelMin());
        mapView.getMapZoomControls().setZoomLevelMax(getZoomLevelMax());

//        mapView.getMapZoomControls().setZoomControlsOrientation(MapZoomControls.Orientation.VERTICAL_IN_OUT);
//        mapView.getMapZoomControls().setZoomInResource(R.drawable.zoom_control_in);
//        mapView.getMapZoomControls().setZoomOutResource(R.drawable.zoom_control_out);
//        mapView.getMapZoomControls().setMarginHorizontal(getResources().getDimensionPixelOffset(R.dimen.controls_margin));
//        mapView.getMapZoomControls().setMarginVertical(getResources().getDimensionPixelOffset(R.dimen.controls_margin));

        //createTileCaches
        tileCaches.add(AndroidUtil.createTileCache(this, getPersistableId(),
                mapView.getModel().displayModel.getTileSize(), getScreenRatio(),
                mapView.getModel().frameBufferModel.getOverdrawFactor(), true));

        //createLayers
        TileRendererLayer tileRendererLayer = AndroidUtil.createTileRendererLayer(tileCaches.get(0),
                mapView.getModel().mapViewPosition, getMapFile(), getRenderTheme(), false, true, false,
                getHillsRenderConfig());
        mapView.getLayerManager().getLayers().add(tileRendererLayer);

        //可以混合加入瓦片图，多图层

        //createControls 设置MapPosition
        mapView.getModel().mapViewPosition.setCenter(new LatLong(32.0607297590, 118.7629586387));
        mapView.getModel().mapViewPosition.setZoomLevel((byte) 16);


    }

    private GroupLayer groupLayer = null;

    private void addOverLays() {
        // Clear overlays
        if (groupLayer != null) {
            mapView.getLayerManager().getLayers().remove(groupLayer);
        } else {
            groupLayer = new GroupLayer();
        }

        Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(getResources().getDrawable(R.drawable.center_marker));
        LatLong pt = new LatLong(32.0607297590, 118.7629586387);
        Tag tag = new Tag("name", "家乐福");
        List<Tag> tags = new ArrayList<>();
        tags.add(tag);
        PointOfInterest pointOfInterest = new PointOfInterest((byte) 5, tags, pt);

        org.mapsforge.map.layer.overlay.Marker marker = new MarkerImpl(pt, bitmap, 0, -bitmap.getHeight() / 2, pointOfInterest);
        groupLayer.layers.add(marker);
        mapView.getLayerManager().getLayers().add(groupLayer);
        mapView.getLayerManager().redrawLayers();
    }

    private class MarkerImpl extends org.mapsforge.map.layer.overlay.Marker {
        private final PointOfInterest pointOfInterest;

        private MarkerImpl(LatLong latLong, Bitmap bitmap, int horizontalOffset, int verticalOffset, PointOfInterest pointOfInterest) {
            super(latLong, bitmap, horizontalOffset, verticalOffset);
            this.pointOfInterest = pointOfInterest;
        }

        @Override
        public boolean onTap(LatLong tapLatLong, org.mapsforge.core.model.Point layerXY, org.mapsforge.core.model.Point tapXY) {
            // GroupLayer does not have a position, layerXY is null
            layerXY = mapView.getMapViewProjection().toPixels(getPosition());
            if (contains(layerXY, tapXY)) {
                Toast.makeText(OSMActivity.this, pointOfInterest.tags.get(0).value, Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    }

    private void updateMap() {
        mapView.getLayerManager().redrawLayers();
    }

    //save MapPosition
    private void save() {
        mapView.getModel().save(preferencesFacade);
        preferencesFacade.save();
    }

    private void destroyMap() {
        AndroidGraphicFactory.clearResourceMemoryCache();
        tileCaches.clear();
    }

//    mapView.getModel().mapViewPosition.zoomIn();
//    mapView.getModel().mapViewPosition.zoomOut();


}
